package cn.neusoft.service.impl;

import cn.neusoft.mapper.UserMapper;
import cn.neusoft.pojo.User;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.UserQuery;
import cn.neusoft.service.IUserService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Transactional
    @Override
    public ResultBean add(User user) {

        if (user==null ||
                StringUtils.isEmpty(user.getUsername())){
            return ResultBean.ERROR("请求参数为空值");
        }

        int add = userMapper.add(user);

        return add>0?ResultBean.SUCCESS(user.getUserId()):ResultBean.ERROR("添加用户信息失败");
    }

    @Transactional
    @Override
    public ResultBean update(User user) {

        if (user==null ||
                user.getUserId()==null || user.getUserId()==0 ||
                StringUtils.isEmpty(user.getUsername())){
            return ResultBean.ERROR("请求参数为空值");
        }

        int update = userMapper.update(user);

        return update>0?ResultBean.SUCCESS():ResultBean.ERROR("修改用户信息失败");
    }

    @Transactional
    @Override
    public ResultBean delete(Integer id) {

        if (id==null){
            return ResultBean.ERROR("请求参数为空值");
        }

//        int delete = userMapper.delete(id);

        //先根据id查询用户
        User user = userMapper.getById(id);

        if (user==null){
            throw new RuntimeException("该用户不存在无法删除");
        }
        //设置删除标记为删除状态
        user.setDelTag(0);
        //进行修改
        int update = userMapper.update(user);

        return update>0?ResultBean.SUCCESS():ResultBean.ERROR("删除用户信息失败");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getById(Integer id) {

        if (id==null){
            return ResultBean.ERROR("请求参数为空值");
        }

        User user = userMapper.getById(id);

        return user!=null?ResultBean.SUCCESS(user):ResultBean.ERROR("查询用户信息失败");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getAll() {
        List<User> userList = userMapper.getAll();
        return ResultBean.SUCCESS(userList);
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getByPage(BaseQuery baseQuery) {

        if (baseQuery == null){
            baseQuery = new UserQuery();
        }

        PageBean<User> pageBean = new PageBean<>();
        int count = userMapper.getCount(baseQuery);
        pageBean.setTotalRows(count);

        List<User> userList = userMapper.getByPage(baseQuery);
        pageBean.setData(userList);


        return ResultBean.SUCCESS(pageBean);
    }

    @Transactional
    @Override
    public ResultBean batchDel(List<Integer> ids) {

        if (ids==null || ids.size()==0){
            return ResultBean.ERROR("请求参数为空值");
        }

        for (Integer id:ids){
            delete(id);
        }

        return ResultBean.SUCCESS();
    }

    @Override
    public ResultBean login(String username, String password) {

        if (username==null || "".equals(username) ||
            password==null || "".equals(password)){
            return ResultBean.ERROR("请求参数为空值");
        }

        //通过用户名查询用户是否存在
        User user = userMapper.getByUsername(username);

        if (user==null){
            return ResultBean.ERROR("用户不存在");
        }

        if(!user.getPassword().equals(password)){
            return ResultBean.ERROR("密码错误");
        }

        //密码不给到前端
        user.setPassword(null);

        return ResultBean.SUCCESS(user);
    }
}
